<template>
	<loading v-if="isLoading" style="height: 70vh;"></loading>
	<view v-else>
		<tn-form :model="seller_config" ref="formRef" :rules="config_formRules" label-position="top">
			<tn-form-item label="增发比例" prop="seller_raise_rate">
				<tn-input type="digit" v-model="seller_config.seller_raise_rate" placeholder="商家货券增发比例（百分比）"
					clearable />
			</tn-form-item>
			<tn-form-item label="增发MAX" prop="seller_raise_max">
				<tn-input type="number" v-model="seller_config.seller_raise_max" placeholder="申请增发券的最大库存限制" clearable />
			</tn-form-item>
			<tn-form-item label="回券比例" prop="seller_back_rate">
				<tn-input type="digit" v-model="seller_config.seller_back_rate" placeholder="增发的券售卖并核销后的返佣比例"
					clearable />
			</tn-form-item>
			<tn-form-item label="平台比例" prop="seller_plat_rate">
				<tn-input type="digit" v-model="seller_config.seller_plat_rate" placeholder="在平台卖出的折扣比例" clearable />
			</tn-form-item>
			<tn-form-item label="扫码比例" prop="seller_alone_rate">
				<tn-input type="digit" v-model="seller_config.seller_alone_rate" placeholder="通过扫码下单的折扣比例" clearable />
			</tn-form-item>
			<tn-form-item label="销售比例" prop="seller_sale_rate">
				<tn-input type="digit" v-model="seller_config.seller_sale_rate" placeholder="针对销售售卖的最低折扣比例" clearable />
			</tn-form-item>
			<tn-form-item label="销售返佣" prop="seller_sale_back_rate">
				<tn-input type="digit" v-model="seller_config.seller_sale_back_rate" placeholder="针对销售售卖的返佣比例(千分比)"
					clearable />
			</tn-form-item>
			<tn-form-item label="套餐比例" prop="seller_combo_rate">
				<tn-input type="digit" v-model="seller_config.seller_combo_rate" placeholder="售卖套餐券的回券比例" clearable />
			</tn-form-item>
			<tn-form-item label="销售套餐" prop="seller_combo_sale_rate">
				<tn-input type="digit" v-model="seller_config.seller_combo_sale_rate" placeholder="针对销售售卖套餐券的返佣比例(千分比) "
					clearable />
			</tn-form-item>
		</tn-form>
		<view class="tn-flex-center">
			<tn-button size="lg" width="80vw" height="80" font-size="26" @click="deliveryConfigBlur">确定修改</tn-button>
		</view>
	</view>
</template>

<script setup>
	import {
		onMounted,
		ref,
	} from 'vue'
	import {
		getconfigItemApi
	} from "@/request/worktop/merchant.js"

	onMounted(() => {
		seller_config.value.id = props.userId
		getconfigItem()
	})

	const props = defineProps(['userId'])

	const isLoading = ref(true)
	const seller_config = ref({
		id: 0,
		seller_raise_rate: '',
		seller_raise_max: '',
		seller_back_rate: '',
		seller_plat_rate: '',
		seller_alone_rate: '',
		seller_sale_rate: '',
		seller_sale_back_rate: '',
		seller_combo_rate: '',
		seller_combo_sale_rate: '',
	})

	// 服务费规则
	const config_formRules = {
		seller_raise_rate: [{
			required: true,
			message: '请输入增发比例',
			trigger: ['change', 'blur']
		}],
		seller_raise_max: [{
			required: true,
			message: '请输入增发MAX',
			trigger: ['change', 'blur']
		}],
		seller_back_rate: [{
			required: true,
			message: '请输入回券比例',
			trigger: ['change', 'blur']
		}],
		seller_plat_rate: [{
			required: true,
			message: '请输入平台比例',
			trigger: ['change', 'blur']
		}],
		seller_alone_rate: [{
			required: true,
			message: '请输入扫码比例',
			trigger: ['change', 'blur']
		}],
		seller_sale_rate: [{
			required: true,
			message: '请输入销售比例',
			trigger: ['change', 'blur']
		}],
		seller_sale_back_rate: [{
			required: true,
			message: '请输入销售返佣',
			trigger: ['change', 'blur']
		}],
		seller_combo_rate: [{
			required: true,
			message: '请输入套餐比例',
			trigger: ['change', 'blur']
		}],
		seller_combo_sale_rate: [{
			required: true,
			message: '请输入销售套餐比例',
			trigger: ['change', 'blur']
		}],
	}

	// 获取商家参数配置
	const getconfigItem = (type = '') => {
		getconfigItemApi(seller_config.value, type).then(res => {
			if (res.code == 0) {
				seller_config.value = res.data
			}
		}).catch(err => {
			uni.showToast({
				title: err.message,
				icon: 'none'
			})
		}).finally(() => {
			setTimeout(() => {
				isLoading.value = false
			}, 500)
		})
	}
	// 修改商家参数配置
	const deliveryConfigBlur = () => {
		seller_config.value.id = seller_config.value.seller_id
		getconfigItemApi(seller_config.value, 'edit').then(res => {
			if (res.code == 0) {
				uni.showToast({
					title: res.message
				})
			}
		}).catch(err => {
			uni.showToast({
				title: err.message,
				icon: 'none'
			})
		})
	}
</script>

<style scoped lang="scss">
</style>